﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SM2Utilities;
using SM2Profile;
using ArtOfTest.WebAii.Controls.HtmlControls;

namespace SM2SetupOptions
{
    public class Dictionary
    {
        public static string GetDictionaryEntries(string strProfile, string strLanguage, string strCategory)
        {
            string listItems = "";
            /*  Load home.aspx is the Profiles Page
             */
            if (!DictionaryActions.is_dictionary_loaded())
            {
                CommonLoader.load("home.aspx");
                ProfileActions.loadProfile(strProfile);
                CommonLoader.load("dictionary.aspx");
            }

            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv pnlContent = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("pnlContent");
            HtmlDiv cphPageContent_upGeneral = pnlContent.Find.ById<HtmlDiv>("cphPageContent_upGeneral");
            HtmlDiv cphPageContent_pnlLingoCategories = cphPageContent_upGeneral.Find.ById<HtmlDiv>("cphPageContent_pnlLingoCategories");
            HtmlSelect cphPageContent_ddlLingoCategoriesLanguages = cphPageContent_pnlLingoCategories.Find.ById<HtmlSelect>("cphPageContent_ddlLingoCategoriesLanguages");
            SM2Input.Tradition.SelectItemSelectList(cphPageContent_ddlLingoCategoriesLanguages, strLanguage);

            //Click to activate the list of categories
                pnlContent = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("pnlContent");
                cphPageContent_upGeneral = pnlContent.Find.ById<HtmlDiv>("cphPageContent_upGeneral");
                cphPageContent_pnlLingoCategories = cphPageContent_upGeneral.Find.ById<HtmlDiv>("cphPageContent_pnlLingoCategories");
                HtmlSelect cphPageContent_ddlLingoCategories1 = cphPageContent_pnlLingoCategories.Find.ById<HtmlSelect>("cphPageContent_ddlLingoCategories");

                    int intLength = cphPageContent_ddlLingoCategories1.Options.Count;
                    if (intLength == 0)
                    {
                        HtmlInputSubmit cphPageContent_btnListDictionaryEntries1 = cphPageContent_pnlLingoCategories.Find.ById<HtmlInputSubmit>("cphPageContent_btnListDictionaryEntries");
                        SM2Input.Tradition.ClickButton(cphPageContent_btnListDictionaryEntries1);

                        Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
                        pnlContent = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("pnlContent");
                        cphPageContent_upGeneral = pnlContent.Find.ById<HtmlDiv>("cphPageContent_upGeneral");
                        cphPageContent_pnlLingoCategories = cphPageContent_upGeneral.Find.ById<HtmlDiv>("cphPageContent_pnlLingoCategories");
                        HtmlInputSubmit cphPageContent_btnListDictionaryEntries2 = cphPageContent_pnlLingoCategories.Find.ById<HtmlInputSubmit>("cphPageContent_btnListDictionaryEntries");
                        while (!cphPageContent_btnListDictionaryEntries2.IsVisible())
                        {
                            System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND);
                            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
                            cphPageContent_upGeneral = pnlContent.Find.ById<HtmlDiv>("cphPageContent_upGeneral");
                            cphPageContent_pnlLingoCategories = cphPageContent_upGeneral.Find.ById<HtmlDiv>("cphPageContent_pnlLingoCategories");
                            cphPageContent_btnListDictionaryEntries2 = cphPageContent_pnlLingoCategories.Find.ById<HtmlInputSubmit>("cphPageContent_btnListDictionaryEntries");
                        }
                    }

            //Select cate item
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            pnlContent = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("pnlContent");
            cphPageContent_upGeneral = pnlContent.Find.ById<HtmlDiv>("cphPageContent_upGeneral");
            cphPageContent_pnlLingoCategories = cphPageContent_upGeneral.Find.ById<HtmlDiv>("cphPageContent_pnlLingoCategories");
            HtmlSelect cphPageContent_ddlLingoCategories = cphPageContent_pnlLingoCategories.Find.ById<HtmlSelect>("cphPageContent_ddlLingoCategories");
            SM2Input.Tradition.SelectItemSelectList(cphPageContent_ddlLingoCategories, strCategory);

            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            pnlContent = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("pnlContent");
            cphPageContent_upGeneral = pnlContent.Find.ById<HtmlDiv>("cphPageContent_upGeneral");
            cphPageContent_pnlLingoCategories = cphPageContent_upGeneral.Find.ById<HtmlDiv>("cphPageContent_pnlLingoCategories");
            HtmlInputSubmit cphPageContent_btnListDictionaryEntries = cphPageContent_pnlLingoCategories.Find.ById<HtmlInputSubmit>("cphPageContent_btnListDictionaryEntries");
            SM2Input.Tradition.ClickButton(cphPageContent_btnListDictionaryEntries);

            //Get number
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            pnlContent = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("pnlContent");
            cphPageContent_upGeneral = pnlContent.Find.ById<HtmlDiv>("cphPageContent_upGeneral");
            cphPageContent_pnlLingoCategories = cphPageContent_upGeneral.Find.ById<HtmlDiv>("cphPageContent_pnlLingoCategories");
            cphPageContent_btnListDictionaryEntries = cphPageContent_pnlLingoCategories.Find.ById<HtmlInputSubmit>("cphPageContent_btnListDictionaryEntries");
            
            while (!cphPageContent_btnListDictionaryEntries.IsVisible())
            {
                System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND);
                Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
                try
                {
                    pnlContent = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("pnlContent");
                    cphPageContent_upGeneral = pnlContent.Find.ById<HtmlDiv>("cphPageContent_upGeneral");
                    cphPageContent_pnlLingoCategories = cphPageContent_upGeneral.Find.ById<HtmlDiv>("cphPageContent_pnlLingoCategories");
                    cphPageContent_btnListDictionaryEntries = cphPageContent_pnlLingoCategories.Find.ById<HtmlInputSubmit>("cphPageContent_btnListDictionaryEntries");
                }
                catch { }
            }

            //Hard-code for now
            System.Threading.Thread.Sleep(5000);

            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            pnlContent = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("pnlContent");
            cphPageContent_upGeneral = pnlContent.Find.ById<HtmlDiv>("cphPageContent_upGeneral");
            HtmlDiv cphPageContent_pnlLingoDictionary = cphPageContent_upGeneral.Find.ById<HtmlDiv>("cphPageContent_pnlLingoDictionary");
            HtmlDiv cphPageContent_pnlDictionaryEntriesScroller = cphPageContent_pnlLingoDictionary.Find.ById<HtmlDiv>("cphPageContent_pnlDictionaryEntriesScroller");
            HtmlTable cphPageContent_gvDictionaryEntries = cphPageContent_pnlDictionaryEntriesScroller.Find.ById<HtmlTable>("cphPageContent_gvDictionaryEntries");

            int intCountRows = cphPageContent_gvDictionaryEntries.Rows.Count;
            for (int i = 1; i < intCountRows; i++)
            {
                HtmlTableRow row = cphPageContent_gvDictionaryEntries.Rows[i];
                HtmlTableCell cell = row[1];
                IList<HtmlSpan> iListSpan = cell.Find.AllByAttributes<HtmlSpan>();

                HtmlSpan spanName = iListSpan[0];
                string strCellName = spanName.TextContent.Trim();
                listItems += strCellName + ",";
            }
            return listItems;
        }
    }
}
